//
//  BluetoothWeighingViewController.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/3/11.
//

#import "BluetoothWeighingViewController.h"
#import "BodyDetailViewController.h"

@interface BluetoothWeighingViewController ()<PPDataInterface, PPBleStateInterface>
@property (weak, nonatomic) IBOutlet UILabel *currentWeightLabel;
@property (weak, nonatomic) IBOutlet UILabel *connectStateLabel;

@property (nonatomic, strong) PPScaleManager *scaleManager;
@property (nonatomic, strong) UserModel *currentUser;
@end

@implementation BluetoothWeighingViewController


+ (instancetype)instance{
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"BluetoothWeighingViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Bluetooth weighing";
    [self newSearch];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scaleManager disconnect];
}


- (void)newSearch{
    self.currentUser = [IdentityManager manager].currentUser;
    PPUserModel *userModel = [[PPUserModel alloc] initWithUserHeight:self.currentUser.userHeight gender:self.currentUser.userGender age:self.currentUser.userAge andUnit:self.currentUser.userUnit];
    NSArray *deviceArr = [DeviceModel allObjects];
    NSMutableArray *macArr = @[].mutableCopy;
    for (DeviceModel *device in deviceArr) {
        [macArr addObject:device.deviceMac];
    }
    self.scaleManager = [[PPScaleManager alloc] initWithMacAddressList:macArr filterDeviceNameArr:nil andUserModel:userModel];
    self.scaleManager.stateInterface = self;
    self.scaleManager.dataInterface = self;
    [self.scaleManager startSearching];

}

/// 监听秤发出的过程数据
- (void)scaleManager:(PPScaleManager *)manager monitorProcessData:(PPBodyBaseModel *)model{
    self.currentWeightLabel.text = [UnitTool weightStrWithScaleName:manager.currentDevice.deviceName weightKg:model.weight / 100.0 andUnitType:self.currentUser.userUnit];
}

- (void)scaleManager:(PPScaleManager *)manager monitorLockData:(PPBodyFatModel *)model {
    PPDeviceModel *device = [manager currentDevice];

    self.currentWeightLabel.text = [UnitTool weightStrWithScaleName:device.deviceName weightKg:model.ppWeightKg andUnitType:self.currentUser.userUnit];
    if (model.ppIsHeartRateEnd) {
        NSString * jsonString = [model mj_JSONString];
        UINavigationController *nav = self.navigationController;
        [nav popViewControllerAnimated:NO];
        BodyDetailViewController *vc = [BodyDetailViewController instance];
        vc.detailStr = [NSString stringWithFormat:@"%@", [jsonString stringByReplacingOccurrencesOfString:@"," withString:@"\n"]];;
        [nav pushViewController:vc animated:YES];
    }

    
}

- (void)scaleManager:(PPScaleManager *)manager monitorHistorData:(PPBodyHistoryBaseModel *)model{
    LFLog(@"%@", model);
}


- (void)monitorBluetoothWorkState:(PPBleWorkState)state{
    switch (state) {
        case PPBleWorkStateSearching:
            self.connectStateLabel.text = @"Scanning";
            break;
        case PPBleWorkStateSearchStop:
            self.connectStateLabel.text = @"Stop scanning";

            break;
        case PPBleWorkStateConnecting:
            self.connectStateLabel.text = @"Connecting";

            break;
        case PPBleWorkStateConnected:
            self.connectStateLabel.text = @"Connected";

            break;
        case PPBleWorkStateDisconnected:
            self.connectStateLabel.text = @"disconnected";
            [self.scaleManager reStartSearching];
            break;
    }
}

- (void)monitorBluetoothSwitchState:(CBManagerState)state API_AVAILABLE(ios(10.0)){
    if (state != CBManagerStatePoweredOn) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Please turn on Bluetooth first" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertController dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }


}

- (void)monitorBluetoothAuthorState:(CBManagerAuthorization)state API_AVAILABLE(ios(13.0)){
    if (state != CBManagerAuthorizationAllowedAlways) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Please enable the Bluetooth permissions first" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertController dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}




@end
